package com.wealth.xtc.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.merchant.entity.Merchant;
import com.wealth.merchant.enums.XTCEnum;
import com.wealth.merchant.service.IMerchantService;
import com.wealth.system.web.controller.BaseController;

/**
 * 限天超
 * @author zjw 
 * 
 */
@Controller
@RequestMapping(value = "/xtc/goods")
public class XtcGoodsController extends BaseController {
	
	Logger logger = Logger.getLogger(XtcGoodsController.class);
	@Autowired
	private IMerchantService iMerchantService;
	
	@Autowired
	IGoodsService iGoodsService;
	/**
	 * 积分商场首页
	 * @return
	 */
	@RequestMapping(value = "/index/{type}")
	public String index(Model model,@PathVariable int type){
		
		try{
			 model.addAttribute("type", type);
			 model.addAttribute("title", XTCEnum.get(type).getComment()); 
		 Page<Goods>  goodsPage = null; 
		 List<Merchant> list = iMerchantService.findMerchantByType(XTCEnum.get(type).getValue());
		 if(list.size()>0){ 
			   
			 long startTime = list.get(0).getStartTime().getTime();
			 long endTime = list.get(0).getEndTime().getTime();
			 long thisTime = System.currentTimeMillis();
			 
			 Goods goods = new Goods();
			 Merchant merchant = new Merchant();
			 merchant.setId(list.get(0).getId());
			 goods.setMerchant(merchant);
			 
			 if(startTime<thisTime && endTime>thisTime){
				 goodsPage =  iGoodsService.getGoodsXTCApi(0, 10,type, goods);
				 model.addAttribute("goodsPage", goodsPage); 
				 if(goodsPage.getContent().size()>0){
						model.addAttribute("sortno", goodsPage.getContent().get(goodsPage.getContent().size()-1).getId());
				 }
			 } 
		 } 
		 
		 if(goodsPage==null || goodsPage.getContent().size()<=0){
			 
			 return "xtcgoods/notstart";
			 
		 }
		if(XTCEnum.X.getValue()==type){
			
			 return "xtcgoods/xlist";
		 }
		if(XTCEnum.T.getValue()==type){
					 
			return "xtcgoods/tlist";
				 }
		if(XTCEnum.C.getValue()==type){
			 
			return "xtcgoods/clist";
		}
		return "system/500";
		}catch(Exception e){
			logger.error("限天超首页错误！！！", e);
			return "system/500";
		}	
	}
	
	
	/**
	  * 限天超商品数据
	  * @param model 
	  * @return
	  */
	@RequestMapping(value = "/xtclistJson",method = RequestMethod.GET )
	@ResponseBody
	public Page<Goods> goodsListJson (Model model,HttpServletRequest request,Goods goods){
		
   try{
   	    String sortno  = request.getParameter("sortno");
   	    Integer type=Integer.valueOf(request.getParameter("type"));
   	    if(sortno==null || sortno ==""){
   	    	sortno = "0"; 
   	    }
   	        Page<Goods>  goodsPage =  iGoodsService.getGoodsXTCApi(Integer.parseInt(sortno), 10,type, goods);
	        model.addAttribute("goodsPage", goodsPage); 
	       
			return goodsPage;
		}catch(Exception e){
			logger.error("限天超商品数据错误！！！", e);
			return null;
		}	
	}
	
}
